version 10.1
#delimit;
set more off;
  quietly log;
  local logon = r(status);
  if "`logon'" == "on" {; log close; };
log using bbdw2012jop-simulate, text replace;


/*	************************************************************************	*/
/*     	File Name:	bbdw2012jop-simulate.do						*/
/*     	Date:   	August 01, 2012							*/
/*      Author: 	Frederick J. Boehmke						*/
/*      Purpose:	Replication file for analysis of exposure to Indian nations 	*/
/*			with and without gaming compacts in California in 1998 and 2000	*/
/*			as in Boehmke, Branton, Dillingham, and Witmer (2012, JOP). 	*/
/*	Input File:	bbdw2012jop.dta							*/
/*      Output File:	bbdw2012jop-simulate.log,					*/
/* 			bbdw2012jop-analyze.gph 					*/
/*	Requires:	CLARIFY 							*/
/*	************************************************************************	*/

	/********************************************************/
	/* This program calculates the marginal effects. 	*/
	/********************************************************/

program define marginals;

		/* Grab the various percentiles for the hypothetical changes. */

	sum gaming_exp_0500, detail;
	
	  local gam_min = r(min);
	  local gam_p10 = r(p10);
	  local gam_p25 = r(p25);
	  local gam_p50 = r(p50);
	  local gam_p75 = r(p75);
	  local gam_p90 = r(p90);
	  local gam_max = r(max);
		
	sum nongam_exp_0500, detail;
	
	  local non_min = r(min);
	  local non_p10 = r(p10);
	  local non_p25 = r(p25);
	  local non_p50 = r(p50);
	  local non_p75 = r(p75);
	  local non_p90 = r(p90);
	  local non_max = r(max);

		/* This loops over the distance of the hypothetical gaming/nongaming tribe. 	*/
		/* Each block of these loops holds the baseline values of gaming/nongaming	*/
		/* exposure at different values. The -display- command at the beginning of 	*/
		/* each should make that clear. Otherwise, they are each the same.		*/
	  	  
	foreach x of numlist 1 5 10 25 50 {;
	  
		display in white "Gaming at min; non-gaming at min; One tribe at `x' miles added then switches.";
		  
			/* Set the values of all variables for CLARIFY. */
			
		setx (Rep_Pres per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime) mean;
		
		setx nongam_exp_0500 `non_min';
		setx gaming_exp_0500 `gam_min';
		setx exposure_gam_nog0500 `gam_min'*`non_min';

			/* This is how much that tribe would change the exposure measures. */
	  
		local change = exp(-.05*`x');
		
		simqi;

			/* First, make it a gaming tribe. */
	  
		setx gaming_exp_0500 `=`gam_min' + `change'';
		setx exposure_gam_nog0500 `=(`gam_min' + `change')*(`non_min')';
			
		display "Adding one gaming tribe at `x' miles";
		
			/* Calculate the baseline vote share and save it in variables. 	*/
			/* $i represents the observation number and is set before each	*/
			/* time the program is called.					*/
		
		simqi, genev(temp);

			sum temp;
			
			replace fd_gam = "min" in $i;
			replace fd_non = "min" in $i;
			replace fd_ev  = r(mean) in $i;
			drop temp;

			/* Calculate the change in the vote share and save it. */
			
		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_min' `=`gam_min' + `change'' 
		  exposure_gam_nog0500 `=(`gam_min')*(`non_min')' `=(`gam_min' + `change')*(`non_min')');
	  
			sum temp;
			
			replace fd_gam = "min" in $i;
			replace fd_non = "min" in $i;
			replace fd_var = "gam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			
			/* Increment $i to save in the next observation. */
			
			global i = $i + 1;
			
			/* Second, make it a nongaming tribe. */
	  
		setx gaming_exp_0500 `gam_min';
		setx nongam_exp_0500 `=`non_min' + `change'';
		setx exposure_gam_nog0500 `=(`gam_min')*(`non_min' + `change')';

		display "Adding one non-gaming tribe at `x' miles";
		simqi;

			/* Calculate the change in the vote share and save it. */
			
		simqi, fd(ev genev(temp)) changex(nongam_exp_0500 `non_min' `=`non_min' + `change''
		  exposure_gam_nog0500 `=(`gam_min')*(`non_min')' `=(`gam_min')*(`non_min' + `change')');
	  
			sum temp;
			
			replace fd_gam = "min" in $i;
			replace fd_non = "min" in $i;
			replace fd_var = "nongam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
			
			/*Third, change it from a nongaming to a gaming tribe. */
	  
		setx nongam_exp_0500 `=`non_min' - `change'';
		setx gaming_exp_0500 `=`gam_min' + `change'';
		setx exposure_gam_nog0500 `=(`gam_min' + `change')*(`non_min' - `change')';
			
		display "Switching one non-gaming tribe to a gaming tribe at `x' miles";
		simqi;

			/* Calculate the change in the vote share and save it. */
			
		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_min' `=`gam_min' + `change'' 
		  nongam_exp_0500 `non_min' `=`non_min' - `change'' 
		  exposure_gam_nog0500 `=(`gam_min')*(`non_min')' `=(`gam_min' + `change')*(`non_min' - `change')');
	  
			sum temp;
			
			replace fd_gam = "min" in $i;
			replace fd_non = "min" in $i;
			replace fd_var = "both" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
			
	  };
	  
		/* Begin next block for different baseline values. */
	  
	foreach x of numlist 1 5 10 25 50 {;
	  
		display in white "Gaming at median; non-gaming at median; One tribe at `x' miles added then switches.";
		  
		setx (Rep_Pres per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime) mean;
		
		setx nongam_exp_0500 `non_p50';
		setx gaming_exp_0500 `gam_p50';
		setx exposure_gam_nog0500 `gam_p50'*`non_p50';

		local change = exp(-.05*`x');
		
		simqi, genev(temp);

			sum temp;
			
			replace fd_gam = "p50" in $i;
			replace fd_non = "p50" in $i;
			replace fd_ev  = r(mean) in $i;
			drop temp;

		setx gaming_exp_0500 `=`gam_p50' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p50' + `change')*(`non_p50')';
			
		display "Adding one gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p50' `=`gam_p50' + `change'' 
		  exposure_gam_nog0500 `=(`gam_p50')*(`non_p50')' `=(`gam_p50' + `change')*(`non_p50')');
	  
			sum temp;
			
			replace fd_gam = "p50" in $i;
			replace fd_non = "p50" in $i;
			replace fd_var = "gam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
	  
		setx gaming_exp_0500 `=`gam_p50'';
		setx nongam_exp_0500 `=`non_p50' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p50')*(`non_p50' + `change')';

		display "Adding one non-gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(nongam_exp_0500 `non_p50' `=`non_p50' + `change''
		  exposure_gam_nog0500 `=(`gam_p50')*(`non_p50')' `=(`gam_p50')*(`non_p50' + `change')');
	  
			sum temp;
			
			replace fd_gam = "p50" in $i;
			replace fd_non = "p50" in $i;
			replace fd_var = "nongam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;

		setx nongam_exp_0500 `=`non_p50' - `change'';
		setx gaming_exp_0500 `=`gam_p50' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p50' + `change')*(`non_p50' - `change')';
		
		display "Switching one non-gaming tribe to a gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p50' `=`gam_p50' + `change'' 
		  nongam_exp_0500 `non_p50' `=`non_p50' - `change'' 
		  exposure_gam_nog0500 `=(`gam_p50')*(`non_p50')' `=(`gam_p50' + `change')*(`non_p50' - `change')');
	  
			sum temp;
			
			replace fd_gam = "p50" in $i;
			replace fd_non = "p50" in $i;
			replace fd_var = "both" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
	  
	  };
	  
	
		/* Begin next block for different baseline values. */
	  
	foreach x of numlist 1 5 10 25 50 {;
	  
		display in white "Gaming at p10; non-gaming at p90; One tribe at `x' miles added then switches.";
		  
		setx (Rep_Pres per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime) mean;
		
		setx nongam_exp_0500 `non_p90';
		setx gaming_exp_0500 `gam_p10';
		setx exposure_gam_nog0500 `gam_p10'*`non_p90';

		local change = exp(-.05*`x');
		
		simqi, genev(temp);

			sum temp;
			
			replace fd_gam = "p10" in $i;
			replace fd_non = "p90" in $i;
			replace fd_ev  = r(mean) in $i;
			drop temp;

		setx gaming_exp_0500 `=`gam_p10' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p10' + `change')*(`non_p90')';
			
		display "Adding one gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p10' `=`gam_p10' + `change'' 
		  exposure_gam_nog0500 `=(`gam_p10')*(`non_p90')' `=(`gam_p10' + `change')*(`non_p90')');
	  
			sum temp;
			
			replace fd_gam = "p10" in $i;
			replace fd_non = "p90" in $i;
			replace fd_var = "gam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
			
		setx gaming_exp_0500 `=`gam_p10'';
		setx nongam_exp_0500 `=`non_p90' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p10')*(`non_p90' + `change')';

		display "Adding one non-gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(nongam_exp_0500 `non_p90' `=`non_p90' + `change''
		  exposure_gam_nog0500 `=(`gam_p10')*(`non_p90')' `=(`gam_p10')*(`non_p90' + `change')');
	  
			sum temp;
			
			replace fd_gam = "p10" in $i;
			replace fd_non = "p90" in $i;
			replace fd_var = "nongam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
		
		setx nongam_exp_0500 `=`non_p90' - `change'';
		setx gaming_exp_0500 `=`gam_p10' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p10' + `change')*(`non_p90' - `change')';
			
		display "Switching one non-gaming tribe to a gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p10' `=`gam_p10' + `change'' 
		  nongam_exp_0500 `non_p90' `=`non_p90' - `change'' 
		  exposure_gam_nog0500 `=(`gam_p10')*(`non_p90')' `=(`gam_p10' + `change')*(`non_p90' - `change')');
	  
			sum temp;
			
			replace fd_gam = "p10" in $i;
			replace fd_non = "p90" in $i;
			replace fd_var = "both" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
	  
	  };
	  
	
		/* Begin next block for different baseline values. */
	  
	foreach x of numlist 1 5 10 25 50 {;
	  
		display in white "Gaming at p90; non-gaming at p10; One tribe at `x' miles added then switches.";
		  
		setx (Rep_Pres per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime) mean;
		
		setx nongam_exp_0500 `non_p10';
		setx gaming_exp_0500 `gam_p90';
		setx exposure_gam_nog0500 `gam_p90'*`non_p10';

		local change = exp(-.05*`x');
		
		simqi, genev(temp);

			sum temp;
			
			replace fd_gam = "p90" in $i;
			replace fd_non = "p10" in $i;
			replace fd_ev  = r(mean) in $i;
			drop temp;

		setx gaming_exp_0500 `=`gam_p90' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p90' + `change')*(`non_p10')';

		display "Adding one gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p90' `=`gam_p90' + `change'' 
		  exposure_gam_nog0500 `=(`gam_p90')*(`non_p10')' `=(`gam_p90' + `change')*(`non_p10')');
	  
			sum temp;
			
			replace fd_gam = "p90" in $i;
			replace fd_non = "p10" in $i;
			replace fd_var = "gam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
		

		display "Adding one non-gaming tribe at `x' miles";
		setx gaming_exp_0500 `=`gam_p90'';
		setx nongam_exp_0500 `=`non_p10' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p90')*(`non_p10' + `change')';

		simqi;

		simqi, fd(ev genev(temp)) changex(nongam_exp_0500 `non_p10' `=`non_p10' + `change''
		  exposure_gam_nog0500 `=(`gam_p90')*(`non_p10')' `=(`gam_p90')*(`non_p10' + `change')');
	  
			sum temp;
			
			replace fd_gam = "p90" in $i;
			replace fd_non = "p10" in $i;
			replace fd_var = "nongam" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;
		
		setx nongam_exp_0500 `=`non_p10' - `change'';
		setx gaming_exp_0500 `=`gam_p90' + `change'';
		setx exposure_gam_nog0500 `=(`gam_p90' + `change')*(`non_p10' - `change')';
			
		display "Switching one non-gaming tribe to a gaming tribe at `x' miles";
		simqi;

		simqi, fd(ev genev(temp)) changex(gaming_exp_0500 `gam_p90' `=`gam_p90' + `change'' 
		  nongam_exp_0500 `non_p10' `=`non_p10' - `change'' 
		  exposure_gam_nog0500 `=(`gam_p90')*(`non_p10')' `=(`gam_p90' + `change')*(`non_p10' - `change')');
	  
			sum temp;
			
			replace fd_gam = "p90" in $i;
			replace fd_non = "p10" in $i;
			replace fd_var = "both" in $i;
			replace fd_dist = `x' in $i;
			replace fd     = r(mean) in $i;
			replace fd_sd  = r(sd) in $i;
			drop temp;
			global i = $i + 1;

	  };
	  
end;


	/****************************************/
	/* Open the data and run the models. 	*/
	/****************************************/

		/* First save an empty data set in which we 	*/
		/* will store our simulated results. 		*/
		
clear;
save bbdw2012jop-simulate, replace emptyok;
	
use bbdw2012jop, clear;

  generat exposure_gam_nog0500 = gaming_exp_0500*nongam_exp_0500;
  generat exposure_cas_noc0500 = casino_exp_0500*noncas_exp_0500;
		
		/* Generate variable names in which to store various results. */
		
  generat fd_model = "";
  generat fd_gam = "";
  generat fd_non = ""; 
  generat fd_ev = .;
  generat fd_var = "";
  generat fd_dist = .;
  generat fd = .;
  generat fd_sd = .;

		/* Create a global to index observation numbers. Each  	*/
		/* simulated result will get stored in one observation.	*/
  
  global i = 1;
  
		/* Run the model for Proposition 5 and then calculate the simulated values. */
  
reg Prop5_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres per_urban_tract 
	med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	if pre2000==1 [aw=pop2000], cluster(fips);

	preserve;
	
	keep if e(sample);
	
	estsimp regress Prop5_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres 
	  per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	  if pre2000==1 [aw=pop2000], cluster(fips);
		
		quietly {; marginals; };
	
		  keep fd*;
		  drop if missing(fd_gam);
		  replace fd_model = "Prop5";
		  append using bbdw2012jop-simulate;
		  save bbdw2012jop-simulate, replace;
	
		restore;

		/* Run the model for Proposition 1 and then calculate the simulated values. */  
	
reg Prop1_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres per_urban_tract 
	med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	if pre2000==0 [aw=pop2000], cluster(fips);

	preserve;
	
	keep if e(sample);
	
	estsimp regress Prop1_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres 
	  per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	  if pre2000==0 [aw=pop2000], cluster(fips);
		
		quietly {; marginals; };
	
		  keep fd*;
		  drop if missing(fd_gam);
		  replace fd_model = "Prop1";
		  append using bbdw2012jop-simulate;
		  save bbdw2012jop-simulate, replace;
	
		restore;

		/* Run the model for Proposition 29 and then calculate the simulated values. */
  		
reg Prop29_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres per_urban_tract 
	med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	if pre2000==0 [aw=pop2000], cluster(fips);

	preserve;
	
	keep if e(sample);
	
	estsimp regress Prop29_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres 
	  per_urban_tract med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	  if pre2000==0 [aw=pop2000], cluster(fips);
		
		quietly {; marginals; };
	
		  keep fd*;
		  drop if missing(fd_gam);
		  replace fd_model = "Prop29";
		  append using bbdw2012jop-simulate;
		  save bbdw2012jop-simulate, replace;
	
		restore;

		
	/************************************************************************/
	/* Open the data set of simulated results to add some information.	*/
	/************************************************************************/

	
use bbdw2012jop-simulate, replace;

  replace fd_ev = fd_ev[_n-1] if missing(fd_ev);
  
  order fd_model fd_gam fd_non fd_var fd_dist fd_ev fd fd_sd;
  
	generat order = 1 if fd_var == "gam";
	replace order = 2 if fd_var == "nongam";
	replace order = 3 if fd_var == "both";
  
  gsort +fd_model +fd_dist -fd_gam +fd_non +order;
  
  outsheet using bbdw2012jop-simulate.csv, comma replace;
	
	
	/************************************************/
	/* Do a counterfactual with no gaming at all. 	*/
	/************************************************/


use bbdw2012jop, clear;
	
  generat exposure_gam_nog0500 = gaming_exp_0500*nongam_exp_0500;	
  
reg Prop5_yes gaming_exp_0500 nongam_exp_0500 exposure_gam_nog0500 Rep_Pres per_urban_tract 
	med_age_tract college_tract inc_tract_1000 white_tract indian_tract crime 
	if pre2000==1 [aw=pop2000], cluster(fips);

	predict yhat1 if e(sample), xb;
	
	  replace nongam_exp_0500 = nongam_exp_0500 + gaming_exp_0500 ;
	  replace gaming_exp_0500 = 0;
	  replace exposure_gam_nog0500 = 0;
	
	predict yhat0 if e(sample), xb;
	
	sum yhat1 yhat0 [aw=pop2000];
	
log close;
clear;
exit, STATA;
